package com.abel.roommanager.service.imp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.abel.roommanager.mapper.RoomMapper;
import com.abel.roommanager.mapper.StudentMapper;
import com.abel.roommanager.pojo.Room;
import com.abel.roommanager.pojo.Student;
import com.abel.roommanager.service.StudentService;
import com.abel.roommanager.util.Page;
@Service
public class StudentServiceImp implements StudentService{
	@Resource
   private StudentMapper studentMapper;
	@Autowired
	private RoomMapper roomMapper;
	@Override
	public Page<Student> findStudentsByCurPage(int curPage) {
		int count=3;
		int index=(curPage-1)*count;
		List<Student> data=studentMapper.findStudentsByCurPage(index,count);
		Page<Student> page=new Page<>();
		page.setData(data);
		page.setCount(count);
		page.setCurrentPage(curPage);
		page.setIndex(index);
		page.setNextPage(curPage+1);
		page.setPrevPage(curPage-1);
		int total=studentMapper.findCountStudents();
		page.setTotal(total);
		int totalSize=(total%count==0)?(total/count):(total/count+1);
		page.setTotalSize(totalSize);
		page.setUrl("jsp/student.jsp");
		return page;
	}
	@Transactional
	@Override
	public int delStu(int id) {
		int result=1;//删除失败
//		根据学生id查询该学生房间
		int  roomno=studentMapper.findRoomnoById(id);
		// 删除该学生信息
		int re=studentMapper.delStuById(id);
		if (re>0) {
//		删除成功，则根据房间编号更新房间的居住人数。
	     int rows= roomMapper.updateLiveNumberByRoomno(roomno,-1);
	     if (rows>0) {
			result=2;//删除学生成功
		   }
		}
		return result;
	}
	@Transactional
	@Override
	public int addStudent(Student stu) {
		int result=0;
//		判断该房间是否已住满
//		根据房间号查询房间
		Room room=roomMapper.findRoomById(stu.getRoomno());
//		判断性别是否可以入住
		if (stu.getSex()!=room.getType()) {
			return 3;
		}
		if (room.getCapacity()<=room.getLivenumber()) {
			return 2;
		}
		int rows=studentMapper.addStudent(stu);
//		如果添加成功则更新room表中房间已居住人数
		if (rows>0) {
			int row=roomMapper.updateLiveNumberByRoomno(stu.getRoomno(),1);
			if (row>0) {
				result=1;
			}
		}
	
		return result;
	}
	@Override
	public Student findStudentById(int id) {
		return studentMapper.findStudentById(id);
	}
	@Transactional
	@Override
	public int updateRoomBySid(Student stu) {
		int result=0;//换房失败
//		根据学生id查询他原来的房间编号
		Student student=studentMapper.findStudentById(stu.getId());
//		根据原来的房间编号查询原来的房间
		Room room=roomMapper.findRoomById(student.getRoom().getId());
//		根据要更新的房间编号查询出要更新的房间
		Room currentRoom=roomMapper.findRoomById(stu.getRoomno());
//		判断原来的房间和现在要换的房间类型是否一致，一致才能更换
		if(room.getType()!=currentRoom.getType()) {
			result=2;//您要换的房间类型与您的性别不匹配
			return result;
		}
//		判断要换的房间的是否已住满，住满则不能换。
		if (currentRoom.getCapacity()<=currentRoom.getLivenumber()) {
			result=3;//该房已住满,请选择别的房间。
			return result;
		}
//		根据学生编号更新房间编号
		int rows=studentMapper.updateRoomnoBySid(stu);
		if (rows>0) {
//		更新成功后，根据现在的房间编号更新房间已居住人数(加一)
			roomMapper.updateLiveNumberByRoomno(stu.getRoomno(), 1);
//		更新原来房间的已居住人数(减一)
			roomMapper.updateLiveNumberByRoomno(student.getRoom().getId(), -1);
			result=1;//换房成功
		}
		
		return result;
	}
	@Override
	public Page<Student> findStudentsByLimits(Student student, int curPage) {
		int count=3;
		int index=(curPage-1)*count;
		List<Student> data=studentMapper.findStudentsByLimitsCurPage(student,index,count);
		Page<Student> page=new Page<>();
		page.setData(data);
		page.setCount(count);
		page.setCurrentPage(curPage);
		page.setIndex(index);
		page.setNextPage(curPage+1);
		page.setPrevPage(curPage-1);
		int total=studentMapper.findCountStudentsByLimits(student);
		page.setTotal(total);
		int totalSize=(total%count==0)?(total/count):(total/count+1);
		page.setTotalSize(totalSize);
		page.setUrl("jsp/student.jsp");
		return page;
	}

}
